package com.sitr.house.controller;

import com.sitr.house.common.constants.CommonConstants;
import com.sitr.house.common.constants.HouseUserType;
import com.sitr.house.common.model.House;
import com.sitr.house.common.model.HouseUser;
import com.sitr.house.common.model.User;
import com.sitr.house.common.model.UserMsg;
import com.sitr.house.common.model.page.PageData;
import com.sitr.house.common.model.page.PageParams;
import com.sitr.house.interceptor.UserContext;
//import com.sitr.house.service.AgencyService;
import com.sitr.house.result.ResultMsg;
import com.sitr.house.service.AgencyService;
import com.sitr.house.service.CityService;
import com.sitr.house.service.HouseService;
import com.sitr.house.service.RecommandService;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.mapping.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.WebParam;
import java.util.List;

@Controller
@Slf4j
public class HouseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    private AgencyService agencyService;

    @Autowired
    private CityService cityService;

    @Autowired
    private RecommandService recommandService;

    /**
     * 1.实现分页
     * 2.支持小区搜索/类型搜搜
     * 3.支持排序
     * 4.支持展示图片、价格、标题、地址等信息
     */
    @RequestMapping("/house/list")
    public String houseList(Integer pageSize, Integer pageNum, House query, ModelMap modelMap) {
//        PageData<House> ps = houseService.queryHouse(query, PageParams.build(pageSize,pageNum));
        PageData<House> ps = houseService.queryHouse(query, PageParams.build(pageSize, pageNum));
        List<House> hotHouses = recommandService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("ps", ps);
        modelMap.put("vo", query);
        System.out.println("这个页面已经正常打印");
        return "house/listing";
    }

    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap map) {
//        System.out.println("如果你可以看到这个 说明这个Controller是没问题的");
        map.put("cities", cityService.getAllCities());
        map.put("communities", houseService.getAllCommunities());
        return "house/add";
    }

    /**
     * 1.获取用户
     * 2.设置房产状态
     *
     * @param
     * @return
     */
    @RequestMapping("house/add")
    public String doAdd(House house) {
        User user = UserContext.getUser();
        house.setState(CommonConstants.HOUSE_STATE_UP);
        houseService.addHouse(house, user);
        return "redirect:/house/ownlist";
    }

    @RequestMapping("house/ownlist")
    public String ownlist(House house, Integer pageNum, Integer pageSize, ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setUserId(user.getId());
        house.setBookmarked(false);
        modelMap.put("ps", houseService.queryHouse(house, PageParams.build(pageSize, pageNum)));
        modelMap.put("pageType", "own");
        return "house/ownlist";
    }

//

    /**
     * 查询房屋详情
     * 查询关联经纪人
     *
     * @param id
     * @return
     */
    @RequestMapping("house/detail")
    public String houseDetail(Long id, ModelMap modelMap) {
        House house = houseService.queryOneHouse(id);
        HouseUser houseUser = houseService.getHouseUser(id);
        recommandService.increase(id);
        if (houseUser.getUserId() != null && !houseUser.getUserId().equals(0)) {
            modelMap.put("agent", agencyService.getAgentDetail(house.getUserId()));
        }
        List<House> hotHouses = recommandService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("house", house);
        return "/house/detail";
    }

    @RequestMapping("house/leaveMsg")
    public String houseMsg(UserMsg userMsg) {
        houseService.addUserMsg(userMsg);
        return "redirect:/house/detail?id=" + userMsg.getHouseId();
    }

    //1 评分
    @ResponseBody
    @RequestMapping("house/rating")
    public ResultMsg houseRate(Double rating, Long id) {
        houseService.updateRating(id, rating);
        return ResultMsg.successMsg("ok");
    }

    //2 收藏
    @ResponseBody
    @RequestMapping("house/bookmark")
    public ResultMsg bookMark(Long id) {
        User user = UserContext.getUser();
        houseService.bindUser2House(id, user.getId(), true);
        return ResultMsg.successMsg("ok");
    }

    //3.删除收藏
    @ResponseBody
    @RequestMapping("house/unbookmark")
    public ResultMsg unBookMark(Long id) {
        User user = UserContext.getUser();
        houseService.unBindUser2House(id, user.getId(), HouseUserType.BOOKMARK);
        return ResultMsg.successMsg("ok");
    }

    @ResponseBody
    @RequestMapping("house/del")
    public String delsale(Long id, String pageType) {
        User user = UserContext.getUser();
        houseService.unBindUser2House(id, user.getId(), pageType.equals("own") ? HouseUserType.SALE : HouseUserType.BOOKMARK);
        return "redirect:/house/ownlist";
    }

    //4.收藏列表
    @RequestMapping("house/bookmarked")
    public String bookmarked(House house, Integer pageNum, Integer pageSize, ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setBookmarked(true);
        house.setUserId(user.getId());
        modelMap.put("ps", houseService.queryHouse(house, PageParams.build(pageSize, pageNum)));
        modelMap.put("pageType", "book");
        return "/house/ownlist";
    }

}
